/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.EstablishmentContactDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class EstablishmentContactDaoImpl extends BaseDaoImpl
		implements
			EstablishmentContactDao {
	public List<?> getList(long estId) {
		String sql = "from com.minierp.model.EstablishmentContact s where s.estId="
				+ estId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List<?> getList(String estType, String searchKey,
			String managerName, String phone, String address, String mark,
			long typeId, long levelId, long areaId, Date startDate,
			Date endDate, boolean checkTs, long tsId, UserBean ub,
			boolean justEnabled) {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = str + " and (" + "(e.estName like :estName) "
					+ " or (e.estCode like :estCode)"
					+ " or (e.nameSpell like :nameSpell)" + ")";
		}

		if (managerName != null) {
			managerName = managerName.trim();
		}

		if (managerName != null && !managerName.equals("")) {
			str = str + " and (e.managerName like :managerName)";
		}

		if (phone != null) {
			phone = phone.trim();
		}

		if (phone != null && !phone.equals("")) {
			str = str + " and (e.phone like :phone)";
		}

		if (address != null) {
			address = address.trim();
		}

		if (address != null && !address.equals("")) {
			str = str + " and (e.address like :address)";
		}

		if (mark != null) {
			mark = mark.trim();
		}

		if (mark != null && !mark.equals("")) {
			str = str + " and (e.mark like :mark)";
		}

		if (typeId != 0L) {
			str = str + " and e.estTypeId=" + typeId;
		}

		if (levelId != 0L) {
			str = str + " and e.estLevelId=" + levelId;
		}

		if (areaId != 0L) {
			str = str + " and e.areaId=" + areaId;
		}

		if (startDate != null) {
			str = str + " and e.createTime>:startDate";
		}

		if (endDate != null) {
			str = str + " and e.createTime<:endDate";
		}

		if (justEnabled) {
			str = str + " and e.enabled=\'Y\'";
		}

		if (checkTs) {
			if (tsId != 0L) {
				str = str + " and e.tallySetId=" + tsId;
			} else {
				str = str
						+ EncCommonHelper.createTallySetSQLStr("e.tallySetId",
								ub, true);
			}
		}

		String sql = "select e.estName,c from com.minierp.model.Establishment e ,com.minierp.model.EstablishmentContact c where e.id=c.estId"
				+ str
				+ " and e.estType=:estType"
				+ " order by e.nameSpell,c.id";
		Session sess = null;

		List arg25;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				String nameSpell = searchKey.toUpperCase();
				q.setString("estName", "%" + searchKey + "%");
				q.setString("estCode", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + nameSpell + "%");
			}

			if (managerName != null && !managerName.equals("")) {
				q.setString("managerName", "%" + managerName + "%");
			}

			if (phone != null && !phone.equals("")) {
				q.setString("phone", "%" + phone + "%");
			}

			if (address != null && !address.equals("")) {
				q.setString("address", "%" + address + "%");
			}

			if (mark != null && !mark.equals("")) {
				q.setString("mark", "%" + mark + "%");
			}

			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			q.setString("estType", estType);
			arg25 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg25;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.EstablishmentContact where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteByEstId(final long estId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.EstablishmentContact where estId=:estId";
						return new Integer(session.createQuery(hql)
								.setLong("estId", estId).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List<?> getEstSmsList(long[] estIdArr) {
		if (estIdArr.length == 0) {
			return new ArrayList();
		} else {
			String idStr = "";

			for (int sql = 0; sql < estIdArr.length; ++sql) {
				if (!idStr.equals("")) {
					idStr = idStr + ",";
				}

				idStr = idStr + estIdArr[sql];
			}

			String arg3 = "select e,c from com.minierp.model.Establishment e ,com.minierp.model.EstablishmentContact c where e.id=c.estId and (c.mobile is not null) and c.mobile!=\'\' and (e.id in ("
					+ idStr + "))";
			return this.getHibernateTemplate().find(arg3);
		}
	}

	public int estCorporate(final long fromClientId, final long toClientId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.EstablishmentContact set estId="
								+ toClientId + " where estId=" + fromClientId;
						Query q = session.createQuery(hql);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int updateTmpCode(final String tmpCode, final long estId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.EstablishmentContact set estId="
								+ estId + " where tmpCode=:tmpCode";
						Query q = session.createQuery(hql);
						q.setString("tmpCode", tmpCode);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}
}